package com.lzj.myvideo.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MediaUtil {

    public MediaUtil(String ffmpegExePath) {
        this.ffmpegExePath = ffmpegExePath;
    }

    private String ffmpegExePath;

    /**
     * 去除原视频音源
     * @param inputVideoPath
     * @throws IOException
     */
    public void removeAudio(String inputVideoPath,String outputPath) throws IOException {
        ArrayList<String> command = new ArrayList<>();
        command.add(ffmpegExePath);
        command.add("-i");
        command.add(inputVideoPath);
        command.add("-c:v");
        command.add("copy");
        command.add("-an");
        command.add(outputPath);
        this.execute(command);
    }
    /**
     * 音视频合并
     * @param inputVideoPath
     * @param inputBgmPath
     * @param videoSeconds
     * @param outputVideoPath
     * @throws IOException
     */
    public void merger(String inputVideoPath,String inputBgmPath,double videoSeconds,String outputVideoPath) throws IOException {
        //ffmpeg.exe -i a.mp4 -i bgm.mp3 -t 10 -y b.mp4
        ArrayList<String> command = new ArrayList<>();
        command.add(ffmpegExePath);
        command.add("-i");
        command.add(inputVideoPath);
        command.add("-i");
        command.add(inputBgmPath);
        command.add("-t");
        command.add(String.valueOf(videoSeconds));
        command.add("-y");
        command.add(outputVideoPath);
        this.execute(command);
    }

    /**
     * 获取视频第一秒的图片
     * @param inputVideoPath
     * @param outputVideoPath
     */
    public void getCover(String inputVideoPath,String outputVideoPath) throws IOException {
        // ffmpeg.exe -ss 00:00:01 -i VID_20191009_162230.mp4 -vframes 1 a.jpg
        ArrayList<String> command = new ArrayList<>();
        command.add(ffmpegExePath);
        command.add("-ss");
        command.add("00:00:01");
        command.add("-y");
        command.add("-i");
        command.add(inputVideoPath);
        command.add("-vframes");
        command.add("1");
        command.add(outputVideoPath);
        this.execute(command);
    }

    /**
     * 传入命令，并执行
     * @param command
     * @throws IOException
     */
    private void execute(ArrayList<String> command) throws IOException {
        //创建命令行
        ProcessBuilder processBuilder = new ProcessBuilder(command);
        //执行命令行
        Process process = processBuilder.start();

        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);
        //将命令行的流推送出去，以免占据内存
        String line = "";
        while ( (line = br.readLine()) != null ) {
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }
    }

}
